package com.javachmapi.springbootjsoup.croner;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.core.SchedulerLock;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;

/**
 * @author songlijun
 * @desc:  采集联联周边游公众号产品信息
 * @Date: 2019\10\9 0009
 */
@Slf4j
//@Service
public class PeptileLlzbyTask {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    //每天凌晨每天凌晨1点执行一次
    @Scheduled(cron = "0 0/1 * * * ?")
    @SchedulerLock(name = "LlzbyJob", lockAtLeastFor = 1000 * 30)
    @Test
    public void LlzbyJob()  {

        //获取当前系统日期
        LocalDate jintian = LocalDate.now();
        String sysdate = String.valueOf(jintian).replace("-","");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info("#####################开始定时采集联联周边游#####################");
        try {
            //第一步、遍历所有城市
            for (int areaId = 0; areaId < 223 ; areaId++) {
                //第二步、获取某个城市的分页量和总数量
                String doc = Jsoup.connect( "https://api.lianlianlvyou.com/v1/wx/list?i=wx45fdcece84d8faf5&locationid=" + areaId )
                        .ignoreContentType(true)
                        .timeout(60000)
                        .execute()
                        .body();
                JSONObject docObject = JSONObject.parseObject( doc );
                //获取城市data.currentSite.city
                String area_name = docObject.getJSONObject( "data" ).getJSONObject( "currentSite" ).getString( "city" );
                //获取每页数量data.list.pageSize
                int pageSize = docObject.getJSONObject( "data" ).getJSONObject( "list" ).getInteger( "pageSize" );
                //获取总数data.list.rowCount
                int rowCount = docObject.getJSONObject( "data" ).getJSONObject( "list" ).getInteger( "rowCount" );
                log.info( "\n每页数量:" + pageSize + "\n总数:" + rowCount );
                for (int pageIndex = 1; pageIndex < rowCount / pageSize + 1; pageIndex++) { //data遍历页数
                    //第三步、首先爬取某个城市周边游的产品数量
                    String res = Jsoup.connect( "https://api.lianlianlvyou.com/v1/wx/list?i=wx45fdcece84d8faf5&locationid=" + areaId + "&pageIndex=" + pageIndex )
                            .ignoreContentType(true)
                            .timeout(60000)
                            .execute()
                            .body();
                    if (StringUtils.isBlank( res )) {
                        log.info( "获取数据失败!" );
                        return;
                    }
                    JSONObject object = JSONObject.parseObject( res );
                    //获取data.list.data
                    JSONArray data = docObject.getJSONObject( "data" ).getJSONObject( "list" ).getJSONArray( "data" );
                    for (int i = 0; i <= data.size()-1; i++) {
                        try {
                            String biz = "联联周边游";
                            String info_id = null;
                            try {
                                info_id = data.getJSONObject(i).getString("id");
                            } catch (Exception e) {
                                log.info("采集info_id出现异常:"+e.getMessage(),e);
                            }
                            String link = null;
                            try {
                                link = "https://api.lianlianlvyou.com/v1/wx/product2?i=wx45fdcece84d8faf5&id="+data.getJSONObject(i).getString("id");
                            } catch (Exception e) {
                                log.info("采集info_id出现异常:"+e.getMessage(),e);
                            }
                            String info_title = null;
                            try {
                                info_title = data.getJSONObject(i).getString("title");
                            } catch (Exception e) {
                                log.info("采集info_title出现异常:"+e.getMessage(),e);
                            }
                            String img = null;
                            try {
                                img = data.getJSONObject(i).getString("faceImg");
                            } catch (Exception e) {
                                log.info("采集img出现异常:"+e.getMessage(),e);
                            }
                            String tree_name = null;
                            String order_start_date = null;
                            String order_end_date = null;
                            String price = "0";
                            try {
                                price=String.valueOf(new Double((Double.valueOf(data.getJSONObject(i).getString("salePriceStr").replace("￥","").trim()))).intValue());
                            } catch (Exception e) {
                                log.info("采集price出现异常:"+e.getMessage(),e);
                            }
                            String market_price = "0";
                            try {
                                market_price=String.valueOf(new Double((Double.valueOf(data.getJSONObject(i).getString("originPriceStr").replace("门市价￥","").trim()))).intValue());
                            } catch (Exception e) {
                                log.info("采集market_price出现异常:"+e.getMessage(),e);
                            }
                            String order_num = null;
                            try {
                                order_num = data.getJSONObject(i).getString("saleAmount");
                            } catch (Exception e) {
                                log.info("采集order_num出现异常:"+e.getMessage(),e);
                            }
                            /**
                             * 得到产品详情相关属性信息
                             */
                            String storage = null;
                            String address = null;
                            String start_date = null;
                            String end_date = null;

                            String context = Jsoup.connect( link)
                                             .data("Authorization","oXQ6s0gvgNYE9ul-bzAKJmBaySHA")
                                             .ignoreContentType(true)
                                             .timeout(10000)
                                             .execute()
                                             .body();
                            if (StringUtils.isBlank( res )) {
                                log.info( "详情获取数据失败!" );
                                return;
                            }
                            JSONObject contextObject = JSONObject.parseObject( context );
                            String phone=null;
                            try {
                                //data.bizProduct.shops[0].phoneNumber
                                phone = contextObject.getJSONObject("data").getJSONObject("bizProduct").getJSONArray("shops").getJSONObject(0).getString("phoneNumber");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            //得到对象中的stockAmount元素
                            try {
                                storage = contextObject.getJSONObject("data").getJSONObject("bizProduct").getString("stockAmount");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            //得到对象中的address元素
                            try {
                                address = contextObject.getJSONObject("data").getJSONObject("bizProduct").getString("address");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            //得到对象中的validBeginDate元素
                            try {
                                start_date = sdf.format(new Date(Long.parseLong(String.valueOf(contextObject.getJSONObject("data").getJSONObject("bizProduct").getString("validBeginDate")))));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            //得到对象中的validEndDate元素
                            try {
                                end_date = sdf.format(new Date(Long.parseLong(contextObject.getJSONObject("data").getJSONObject("bizProduct").getString("validEndDate"))));   // 时间戳转换成时间
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            try {
                                jdbcTemplate.update("insert into reptile_product_zby_"+sysdate+" (biz,info_id,link,info_title,img,area_name,tree_name,order_start_date,order_end_date,price,market_price,order_num,address,phone,storage,start_date,end_date,context)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                                        new Object[]{biz,info_id,link,info_title,img,area_name,tree_name,order_start_date,order_end_date,price,market_price,order_num,address,phone,storage,start_date,end_date,context});
                                log.info("正在爬取"+area_name+"第"+i+"件产品");
                            } catch (Exception e) {
                                log.info("插入数据库出现异常:"+e.getMessage(),e);
                            }
                        } catch (Exception e) {
                            log.info("采集遍历城市出现错误，原因："+e.getMessage(),e);
                        }
                    }
                }
            }
        }catch (Exception e){
            log.error("采集响应失败,原因:"+e.getMessage(),e);
        }
    }

    public static void main(String[] args) {
        try{
            String doc = Jsoup.connect("https://api.lianlianlvyou.com/v1/wx/list?i=wx45fdcece84d8faf5&locationid=59")
                              .timeout(1000)
                              .execute()
                              .body();
            System.out.println(doc);
        }catch (Exception e){

        }
    }
}